package com.example.octopus.service;

import com.example.octopus.entity.*;
import com.example.octopus.entity.returnMessage.DeleteReturnMessage;
import com.example.octopus.entity.returnMessage.InsertReturnMessage;
import com.example.octopus.entity.returnMessage.SelectReturnMessage;
import com.example.octopus.entity.returnMessage.UpdateReturnMessage;
import com.example.octopus.utils.DataBaseUtils;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.Charset;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class DataBaseServiceImpl implements DataBaseService{
    //稍后yml装填
    private static final String APP_ID ="wx6fc554a81e446f79";
    private static final String SECRET ="b4cd354d99456456880fea335cd6ab00";
    private static final int PAGESIZE=10;
    //

    private WeixinAccessTokenService weixinAccessTokenService;
    @Override
    public WeixinAccessToken getWeixinAccessToken() {
        return weixinAccessTokenService.getAccessToken(APP_ID,SECRET);
    }

    private WeixinAccessToken weixinAccessToken;
    private Gson gson;
    @Autowired
    public DataBaseServiceImpl(Gson gson,WeixinAccessTokenService weixinAccessTokenService){
        this.weixinAccessTokenService=weixinAccessTokenService;
        this.gson=gson;
        weixinAccessToken=this.weixinAccessTokenService.getAccessToken(APP_ID,SECRET);
    }
    @Override
    public <T> InsertReturnMessage insert(String collection, String env, List<T> data) {
        String url = "https://api.weixin.qq.com/tcb/databaseadd?access_token=" + weixinAccessTokenService.getAccessToken(APP_ID,SECRET).getAccessToken();
        QueryObject queryObject=new QueryObject();
        queryObject.setEnv(env);
        queryObject.setQuery(DataBaseUtils.insert(collection,data));
        String gsonQuery=gson.toJson(queryObject);
        System.out.println(gsonQuery);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        HttpEntity<String> entity = new HttpEntity<>(gsonQuery, headers);// 设置 Accept 字段接受 UTF-8 编码的内容
        ResponseEntity<InsertReturnMessage> response = restTemplate.postForEntity(url,entity,InsertReturnMessage.class);
        return response.getBody();
    }

    @Override
    public DeleteReturnMessage delete(String collection, String env, Map<String, Object> condition) {
        String url ="https://api.weixin.qq.com/tcb/databasedelete?access_token="+weixinAccessTokenService.getAccessToken(APP_ID,SECRET).getAccessToken();
        QueryObject queryObject=new QueryObject();
        queryObject.setEnv(env);
        queryObject.setQuery(DataBaseUtils.delete(collection,condition));
        String gsonQuery=gson.toJson(queryObject);
        System.out.println(gsonQuery);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        HttpEntity<String> entity = new HttpEntity<>(gsonQuery, headers);// 设置 Accept 字段接受 UTF-8 编码的内容
        ResponseEntity<DeleteReturnMessage> response = restTemplate.postForEntity(url,entity,DeleteReturnMessage.class);
        return response.getBody();
        // return  DataBaseUtils.select("store",condition);
    }

    @Override
    public SelectReturnMessage select(String collection, String env, Map<String, Object> condition, int limit, int offset) {
        String url ="https://api.weixin.qq.com/tcb/databasequery?access_token="+weixinAccessTokenService.getAccessToken(APP_ID,SECRET).getAccessToken();
        QueryObject queryObject=new QueryObject();
        queryObject.setEnv(env);
        queryObject.setQuery(DataBaseUtils.select(collection,condition,limit,offset));
        String gsonQuery=gson.toJson(queryObject);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        HttpEntity<String> entity = new HttpEntity<>(gsonQuery, headers);// 设置 Accept 字段接受 UTF-8 编码的内容
        ResponseEntity<SelectReturnMessage> response = restTemplate.postForEntity(url,entity,SelectReturnMessage.class);
        return response.getBody();
    }
    @Override
    public SelectReturnMessage select(String collection, String env, int page) {
        int offset=PAGESIZE*page;
        String url ="https://api.weixin.qq.com/tcb/databasequery?access_token="+weixinAccessTokenService.getAccessToken(APP_ID,SECRET).getAccessToken();
        QueryObject queryObject=new QueryObject();
        queryObject.setEnv(env);
        queryObject.setQuery(DataBaseUtils.select(collection,offset));
        String gsonQuery=gson.toJson(queryObject);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        HttpEntity<String> entity = new HttpEntity<>(gsonQuery, headers);// 设置 Accept 字段接受 UTF-8 编码的内容
        ResponseEntity<SelectReturnMessage> response = restTemplate.postForEntity(url,entity,SelectReturnMessage.class);
        return response.getBody();
    }
    @Override
    public SelectReturnMessage select(String collection, String env, Map<String, Object> condition, int page) {
        int offset=PAGESIZE*page;
        String url ="https://api.weixin.qq.com/tcb/databasequery?access_token="+weixinAccessTokenService.getAccessToken(APP_ID,SECRET).getAccessToken();
        QueryObject queryObject=new QueryObject();
        queryObject.setEnv(env);
        queryObject.setQuery(DataBaseUtils.select(collection,condition,offset));
        String gsonQuery=gson.toJson(queryObject);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        HttpEntity<String> entity = new HttpEntity<>(gsonQuery, headers);// 设置 Accept 字段接受 UTF-8 编码的内容
        ResponseEntity<SelectReturnMessage> response = restTemplate.postForEntity(url,entity,SelectReturnMessage.class);
        return response.getBody();
    }
    @Override
    public SelectReturnMessage select(String collection, String env, Map<String, Object> condition) {
        String url ="https://api.weixin.qq.com/tcb/databasequery?access_token="+weixinAccessTokenService.getAccessToken(APP_ID,SECRET).getAccessToken();
        QueryObject queryObject=new QueryObject();
        queryObject.setEnv(env);
        queryObject.setQuery(DataBaseUtils.select(collection,condition));
        String gsonQuery=gson.toJson(queryObject);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        HttpEntity<String> entity = new HttpEntity<>(gsonQuery, headers);// 设置 Accept 字段接受 UTF-8 编码的内容
        ResponseEntity<SelectReturnMessage> response = restTemplate.postForEntity(url,entity,SelectReturnMessage.class);
        return response.getBody();
    }

    @Override
    public SelectReturnMessage select(String collection, String env) {
        String url ="https://api.weixin.qq.com/tcb/databasequery?access_token="+weixinAccessTokenService.getAccessToken(APP_ID,SECRET).getAccessToken();
        QueryObject queryObject=new QueryObject();
        queryObject.setEnv(env);
        queryObject.setQuery(DataBaseUtils.select(collection));
        String gsonQuery=gson.toJson(queryObject);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        HttpEntity<String> entity = new HttpEntity<>(gsonQuery, headers);// 设置 Accept 字段接受 UTF-8 编码的内容
        ResponseEntity<SelectReturnMessage> response = restTemplate.postForEntity(url,entity,SelectReturnMessage.class);
        return response.getBody();
    }
    @Override
    public  UpdateReturnMessage update(String collection, String env,  String docId,Object data){
        String url ="https://api.weixin.qq.com/tcb/databaseupdate?access_token="+weixinAccessTokenService.getAccessToken(APP_ID,SECRET).getAccessToken();
        QueryObject queryObject=new QueryObject();
        queryObject.setEnv(env);
        queryObject.setQuery(DataBaseUtils.update(collection,docId,data));
        String gsonQuery=gson.toJson(queryObject);
        System.out.println(gsonQuery);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        HttpEntity<String> entity = new HttpEntity<>(gsonQuery, headers);// 设置 Accept 字段接受 UTF-8 编码的内容
        ResponseEntity<UpdateReturnMessage> response = restTemplate.postForEntity(url,entity,UpdateReturnMessage.class);
        return response.getBody();
    }
    // no use
    @Override
    public SelectReturnMessage select(String collection, String env, String condition){
        String url ="https://api.weixin.qq.com/tcb/databasequery?access_token="+weixinAccessTokenService.getAccessToken(APP_ID,SECRET).getAccessToken();
        QueryObject queryObject=new QueryObject();
        queryObject.setEnv(env);
        queryObject.setQuery(DataBaseUtils.select(collection,condition));
        String gsonQuery=gson.toJson(queryObject);
        System.out.println(gsonQuery);
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8); // 设置 Content-Type 为 application/json 并指定 UTF-8 编码
        headers.setAcceptCharset(Collections.singletonList(Charset.forName("UTF-8")));
        HttpEntity<String> entity = new HttpEntity<>(gsonQuery, headers);// 设置 Accept 字段接受 UTF-8 编码的内容
        ResponseEntity<SelectReturnMessage> response = restTemplate.postForEntity(url,entity,SelectReturnMessage.class);
        return response.getBody();
    }
    // 查询对象类
    static class QueryObject {
        private String env;

        public String getEnv() {
            return env;
        }

        public void setEnv(String env) {
            this.env = env;
        }

        public String getQuery() {
            return query;
        }

        public void setQuery(String query) {
            this.query = query;
        }
        private String query;
        // 可以添加其他需要的字段

    }
}
